package com.ihd.ihardware.common.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import cn.wowjoy.commonlibrary.utils.DensityUtil;

/**
 * Created by 84429 on 2017/7/2.
 */

public class BtnRelativeLayout extends RelativeLayout {
    private int lastX;
    private int upX;
    private boolean isTouching = false;
    private OnCLickListener listener = null;

    public interface OnCLickListener {
        void onClick(int level);
    }

    public void setOnCLickListener(OnCLickListener listener) {
        this.listener = listener;
    }

    public BtnRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        Log.e("onTouchEvent", event.getX() + "  " + event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouching = true;
                lastX = (int) event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                isTouching = true;
                int dx = (int) event.getRawX() - lastX;
                int l = getLeft() + dx;
                int r = getRight() + dx;
                if (l < 0) {
                    l = 0;
                    r = l + getWidth();
                }
                if (r > DensityUtil.dip2px(150)) {
                    r = DensityUtil.dip2px(150);
                    l = r - getWidth();
                }
                layout(l, getTop(), r, getBottom());
                lastX = (int) event.getRawX();
                upX = r;
                postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isTouching = false;
                    }
                },1000);
                LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.CENTER_VERTICAL);
                if (upX < DensityUtil.dip2px(74)) {
                    if (listener != null)
                        listener.onClick(1);
                    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                } else if (upX >= DensityUtil.dip2px(74) && upX <= DensityUtil.dip2px(124)) {
                    lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
                    if (listener != null)
                        listener.onClick(2);
                } else {
                    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    if (listener != null)
                        listener.onClick(3);
                }
                setLayoutParams(lp);
                postInvalidate();
                break;
        }
        return true;
    }

    public boolean isTouching() {
        return isTouching;
    }

    public void setLevel(int i) {
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_VERTICAL);
        if (i == 1) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        } else if (i == 2) {
            lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        } else if (i == 3) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        setLayoutParams(lp);
        postInvalidate();
    }
}
